﻿using Application.PositionService;
using Domain.Entity.zct;
using Domain.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Application.DepartmentService;

namespace WMSManage.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PositionController : ControllerBase
    {
        private readonly IPositionService positionService;

        public PositionController(IPositionService positionService)
        {
            this.positionService = positionService;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="addPositionDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public Task<int> GetAdd(AddPositionDto addPositionDto)
        {
            return positionService.GetAdd(addPositionDto);
        }

        /// <summary>
        /// 全部查
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<List<Position>> GetAll()
        {
            return positionService.GetAll();
        }

        /// <summary>
        /// 条件查
        /// </summary>
        /// <param name="fenYe"></param>
        /// <param name="Position_code"></param>
        /// <param name="Position_name"></param>
        /// <param name="Position_status"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<FenYe<Position>> GetCondition(int CurrentPage, string? Position_code, string? Position_name, int? Position_status)
        {
            FenYe<Position> fenYe = new FenYe<Position>()
            {
                CurrentPage = CurrentPage,
            };
            return positionService.GetCondition(fenYe,Position_code, Position_name, Position_status);
        }

        /// <summary>
        /// Id查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<Position> GetID(int id)
        {
            return positionService.GetID(id);
        }

        /// <summary>
        /// 停用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> GetDeactivated(int id)
        {
            return positionService.GetDeactivated(id);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public async Task<IActionResult> GetDelete(int id)
        {
            try
            {
                var result = await positionService.GetDelete(id);

                // 根据 Repository 返回的 int 结果，返回不同的 HTTP 响应
                return result switch
                {
                    1 => Ok(new { code = 1, message = "部门删除成功" }),
                    0 => BadRequest(new { code = 0, message = "删除失败：该部门已被其他单据引用，无法删除" }),
                    -1 => NotFound(new { code = -1, message = "部门不存在或已被删除" }),
                    _ => StatusCode(500, new { code = -2, message = "服务器内部错误：未知的删除结果" })
                };
            }
            catch (System.Exception ex)
            {
                // 记录异常日志 (如果配置了日志系统)
                // _logger.LogError(ex, "删除部门 {SupplierId} 时发生异常", id);
                return StatusCode(500, new { code = -2, message = $"服务器内部错误: {ex.Message}" });
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="updatePositionDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> GetEdit(UpdatePositionDto updatePositionDto)
        {
            return positionService.GetEdit(updatePositionDto);
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> GetEnable(int id)
        {
            return positionService.GetEnable(id);
        }
    }
}
